package com.ebt.m.proposal_v2.utils;

import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 验证
 *
 * @author archer.qi
 *         Created on 2017/5/8.
 */
public class VerificationUtils {

    private static boolean matcher(String regex, String target) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(target);
        return matcher.matches();
    }

    /**
     * 生日验证
     *
     * @param birthday
     * @return
     */
    public static boolean isBirthday(String birthday, String split) {
        String regex = "[0-9]{4}" + split + "[0-9]{1,2}" + split + "[0-9]{1,2}";
        boolean formatMatcher = matcher(regex, birthday);
        if (formatMatcher) {
            int[] values = ArrayUtils.toBirthdayArray(birthday, split);
            if (values[0] < 1900 || values[0] > Calendar.getInstance().get(Calendar.YEAR)) {
                return false;
            }
            if (values[1] > 12 || values[1] < 1) {
                return false;
            }
            if (values[2] > 31 || values[2] < 1) {
                return false;
            }
            return true;
        }
        return false;
    }
}
